<?php

namespace app\sys\controller;

use think\Db;

// 后台 帮会管理
class Gang extends Admin
{
    // 帮会列表
    public function index()
    {
        $name = input('param.name', '');
        $phone = input('param.phone', '');
        $csid = input('param.csid', '');
        $date = input('param.date', '');
        $page = input('param.page', 1);
        $page = $page <= 0 ? 1 : $page;
        $limit = input('param.limit', 20);
        $order_field = input('param.field', '');
        $order_type = input('param.order', 0);
        $admin_info = session('admin_info');
        if (request()->isAjax() || request()->isPost()) {
            if ($admin_info['permission'] < 10 && 3 != $admin_info['permission']) {
                return error('你的权限不够');
            }
            $query = [];
            if (3 == $admin_info['permission']) {
                $query['csid'] = $admin_info['id'];
            }
            if ($name) {
                $query['name'] = ['like', $name];
            }
            if ($phone) {
                $query['phone'] = ['like', $phone];
            }
            if ($csid) {
                $query['csid'] = $csid;
            }
            if ($date) {
                $arr = explode('~', $date);
                $query['ct'] = [['gte', strtotime(trim($arr[0]))], ['lte', strtotime(trim($arr[1]).'23:59:59')]];
            }
            $order = ['ct' => -1];
            if ($order_field && $order_type) {
                $order[$order_field] = 'asc' == $order_type ? 1 : -1;
            }
            $list = Db::name('gang')->where($query)->page($page)->limit($limit)->order($order)->select();
            foreach ($list as $key => $value) {
                if ($value['csid']) {
                    $admin_info = Db::name('admin')->find($value['csid']);
                    $list[$key]['cs_name'] = $admin_info['username'];
                }
                if ($value['type_id']) {
                    $type_info = Db::name('gang_type')->find($value['type_id']);
                    $list[$key]['type_name'] = $type_info['name'];
                }
                if ($value['user_id']) {
                    $user_info = Db::name('user')->find($value['user_id']);
                    $list[$key]['user_name'] = $user_info['name'];
                }
                $list[$key]['ct'] = date('Y-m-d H:i:s', $value['ct']);
            }
            $count = Db::name('gang')->where($query)->count();

            return success(['data' => $list, 'count' => $count]);
        }
        $cs_list = Db::name('admin')->where(['permission' => 3])->field('id,username')->select();

        return view('index', ['page' => $page, 'limit' => $limit, 'name' => $name, 'cs_list' => $cs_list]);
    }

    // 帮会编辑
    public function gangEdit()
    {
        if (request()->isPost()) {
            $data = input('param.', '');
            $row = [];
            $row['csid'] = strval($data['csid']);
            if (!$row['csid'] || 24 != strlen($row['csid'])) {
                return error('请选择专属客服');
            }
            $row['type_id'] = strval($data['type_id']);
            if (!$row['type_id'] || 24 != strlen($row['type_id'])) {
                return error('请选择帮会分类');
            }
            $id = $data['id'];

            Db::name('gang')->where('id', $id)->update($row);

            return success();
        }

        $id = input('param.id', '');
        $data = Db::name('gang')->find($id);
        $cs_list = Db::name('admin')->where(['permission' => 3])->field('id,username')->select();
        $type_list = Db::name('gang_type')->field('id,name')->select();

        return view('gangEdit', ['info' => $data, 'cs_list' => $cs_list, 'type_list' => $type_list]);
    }

    // 帮会删除
    public function gangDel()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            Db::name('gang')->delete($id);

            return success();
        }

        return error();
    }

    // 开通帮会、关闭帮会
    public function gangOpen()
    {
        if (request()->isPost()) {
            $admin_info = session('admin_info');
            if ($admin_info['permission'] < 10 && 3 != $admin_info['permission']) {
                return error('你的权限不够');
            }
            $id = input('param.id', '');
            $disable = input('param.disable', '');
            $disable = 'true' == $disable ? 0 : 1;
            Db::name('gang')->where('id', $id)->update(['disable' => $disable]);
            $gang_info = Db::name('gang')->where('id', $id)->find();
            Db::name('user')->where('id', $gang_info['user_id'])->update(['gang_id' => 0 == $disable ? $id : '', 'gang_role' => 0 == $disable ? 5 : 0]);
            cache('api_user_info_'.$gang_info['user_id'], null);
            // 开通的时候上架全部产品
            if (0 == $disable) {
                autoPutaway(null, [$gang_info]);
                // 发送模板消息 小程序(没有公众号模板)
                $wx_service = new \wx\WeixinService();
                $u = Db::name('user')->where('id', $gang_info['user_id'])->field('name,openid')->find();
                $tData = [
                    'keyword1' => ['value' => $u['name']],
                    'keyword2' => ['value' => '您申请的【'.$gang_info['name'].'】帮会已经开通'],
                ];
                $res = $wx_service->sendTemplateMsg($u['openid'], $gang_info['form_id'], 'LIW_kv2FmnDEmVnqfS0QNwe81sRPUeiyaNzlRgY_f0E', 'pages/index/welcome', $tData);

                // tp没有$unset 删除字段
                Db::name('gang')->where('id', $id)->update(['form_id' => '']);
                // 开通小店(删除原来的店 先开个店)
                Db::name('shop')->where('user_id', $gang_info['user_id'])->delete();
                $new_id = Db::name('shop')->insert(['user_id' => $gang_info['user_id'], 'ct' => time(), 'gang_id' => $id, 'score' => 0, 'sales' => 0, 'evaluate_num' => 0]);
                Db::name('user')->where('id', $gang_info['user_id'])->update(['shop_id' => $new_id]);

                return success($res);
            }

            return success();
        }

        return error();
    }
}
